﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Utility.BehaviourTree
{
	public class FailOnTimeLimitNode : DecoratorNode
	{
		float waitTime = 0;

		float targetTime = -1;

		public FailOnTimeLimitNode (float waitTime)
		{
			this.waitTime = waitTime < 0 ? 0 : waitTime;
		}

		protected override bool OnStart ()
		{
			targetTime = DataBoard.Time + waitTime;
			return true;
		}

		protected override Status OnUpdate ()
		{
			switch (ChildNode.Update ())
			{
				case Status.Success:
					return Status.Success;
				case Status.Failure:
					return Status.Failure;
				case Status.Error:
					return Status.Error;
			}
			//check timeout
			if (DataBoard.Time >= targetTime)
			{
				return Status.Failure;
			}
			else
			{
				return Status.Running;
			}
		}

		protected override void OnReset ()
		{
			base.OnReset ();
			targetTime = -1;
		}
	}
}